import Mock from 'mockjs'
import Utils from "@/utils/basic.utils";
import store from '@/store'
import moment from "moment";
const symbol = '~!@#$%^&*()_+<>?'
const english = 'abcdefghijklmnopqrstuvwxyz'
const china = '我是中国人你是哪里的为什么会这样非常棒偏僻子'
Mock.setup({
    timeout: '20-380'
})

Mock.Random.extend({
    phone: function () {
        let phonePrefix = ['132', '135', '189', '180', '158']
        return this.pick(phonePrefix) + Mock.mock(/\d{8}/) //Number()
    }
})
function text(model, template) {
    let fieldName = model['field'];
    if(fieldName.includes("name")) {
        if(fieldName.includes('account')) {
            template[model.id]=`@string(${english}, 3, 8)`;
        } else if(fieldName.includes("user") || fieldName.includes("real")) {
            template[model.id]='@cname'
        } else {
            template[model.id]=`@string(${china}, 3, 8)`;
        }
    } else if(fieldName.includes("phone")) {
        template[model.id]='@phone'
    } else if(fieldName.includes("password")) {
        template[model.id] = () => {
            return Mock.Random.string(english + symbol, 8, 15);
        }
    } else {
        template[model.id] = () => {
            return Mock.Random.string(english, 3, 8);
        }
    }
}

function radio(model, template) {
    template[model.id+"|1"] = model.options.map(item => item.value)
}
function select(model, template) {
    template[model.id+"|1"] = model.options.map(item => item.value)
}
function date(model, template) {
    template[model.id] = () => {
        let date = Mock.Random.date();
        return moment(date).format(model.format)
    }
}

function slider(model, template) {
    template[model.id] = `@integer(${model.min}, ${model.max})`
}

function number(model, template) {
    template[model.id] = `@float(${model.min}, ${model.max}, 0, 3)`
}

function checkbox(model, template) {

}
function rate(model, template) {
    template[model.id] = `@integer(1, ${model.count})`
}

const MockMetaLib = {date, checkbox, text, radio, slider, number, rate, select}

/**
 *  解析编辑元信息生产列表mock模板
 */
function resolveMetasToTable() {
    let metas = store.state.edit.metas;
    if(Utils.isBlank(metas)) {
        return {};
    }

    let mockTemplate = {'id|+1': 1};
    metas.forEach(meta => {
        let model = meta['tabModel'];
        let mockFunction = MockMetaLib[meta.type];
        if(typeof mockFunction == 'function') {
            mockFunction(model, mockTemplate)
        } else {
            text(model, mockTemplate);
        }
    })

    return mockTemplate;
}
/**
 * 数据模拟
 */
Mock.mock(RegExp('/preview/view'), 'get', args => {
    let {size, current} = Utils.resolverQueryOfUrl(args.url);
    let preview = store.getters.preview;
    let data = preview.dataSource;
    if(preview.reset) {
        let template = resolveMetasToTable()
        if(template == null) {
            return {code: 500, message: '模拟模板不存在'};
        }

        preview.reset = false; // 在关闭预览的时候详单与重置将置为true
        preview.dataSource = data = Mock.mock({"data|3-27": [template]}).data;
    }

    return {
        code: 200, message: 'OK', data: {size: size, records: data, total: data.length}
    }
})

Mock.mock(RegExp('/preview/edit'), 'get', args => {
    let {id} = Utils.resolverQueryOfUrl(args.url);
    let preview = store.getters.preview;
    let data = preview.dataSource;
    let detail = data.find(item => item.id == id);
    return {
        code: 200, message: 'OK', data: detail
    }
})
Mock.mock(RegExp('/preview/edit'), 'post', args => {
    let entity = JSON.parse(args.body);
    let preview = store.getters.preview;
    let data = preview.dataSource;
    if(entity.id) {
        let detail = data.find(item => item.id == entity.id);
        Object.assign(detail, entity);
    } else {
        let last = data[data.length - 1];
        entity.id = last.id + 1;
        preview.dataSource.splice(0, 0, entity);
    }
    return {
        code: 200, message: 'OK'
    }
})
/**
 * 数据模拟
 */
Mock.mock(RegExp('/preview/del'), 'post', args => {
    let ids = JSON.parse(args.body);
    let preview = store.getters.preview;
    let data = preview.dataSource;
    let index = data.findIndex(item => item.id == ids[0]);
    if(index > -1) {
        data.splice(index, 1);
    }
    return {
        code: 200, message: 'OK'
    }
})